<template>
	<view class="login">
		<image class="app-logo" :src="imgUrl+'logo.png'" mode=""></image>
		<view class="slogan">欢迎登录迪小星</view>
		<image class="loginLink" :src="imgUrl+'loginLink.png'" mode=""></image>



		<button v-if="!loginState && JSON.stringify(userInfo) === '{}'" @tap="weChatLogin" plain border="none"
			class="loginBtn">
			微信授权登录
		</button>

		<button v-else plain border="none" class="loginBtnBack">
			{{getreviewStatusName()}}
		</button>

		<!-- 	<button plain border="none" class="phoneBtn">
			手机号登录
		</button> -->
		<view class="privacyBox">
			<label class="radio" @click="privacyState = !privacyState">
				<radio value="r1" :checked="privacyState" color="#00bf39" style="transform:scale(0.7)" />已阅读并同意 <text
					style="color:#00bf39;" @tap.stop="goAboutUs(1)">《迪小星用户协议》</text> 和 <text style="color:#00bf39;"
					@tap.stop="goAboutUs(2)">《隐私政策》</text>
			</label>
		</view>
	</view>

	</view>
	</view>
</template>

<script>
	import {
		sendMsg,
		signIn,
		getOpenId,
		wxLogin,
	} from '@/api/api.js'
	export default {
		data() {
			return {
				privacyState: false,
				imgUrl: this.$imgUrl,
				defultCurrent: 0,
				isPasswordShow: true,
				// 账密
				account: '',
				password: '',
				// 手机号/验证码
				phone: '',
				authcode: '',
				tips: '',
				// 公共信息
				commenDetail: {},
				xieyiShow: false,
				yinsiShow: false,
				// openId
				openId: '',
				userInfo: {},
				loginState: false,
				reviewStatus: [{
						id: 1,
						name: '待审核'
					},
					{
						id: 2,
						name: '巳审核'
					},
					{
						id: 3,
						name: '审核拒绝'
					}
				],
			};
		},
		onShow() {
			// this.commenInfo()
		},
		methods: {
			getreviewStatusName() {
				const value = this.userInfo.reviewStatus || 1;
				return this.reviewStatus.find(item => item.id == value).name
			},
			goAboutUs(type) {
				uni.navigateTo({
					url: `/package/pages/aboutUs/aboutUs?type=${type}`
				})
			},
			xieyiclose() {
				this.xieyiShow = false
			},
			yinsiclose() {
				this.yinsiShow = false
			},
			commenInfo() {
				getCommon({}, {
					custom: {
						auth: false
					}
				}).then(res => {
					if (res.code == 200) {
						this.commenDetail = res.data
					}
				})
			},
			testPhone(val) {
				if (!uni.$u.test.mobile(val)) {
					uni.$u.toast('请输入正确的手机号')
				}
			},
			toRegister() {
				uni.navigateTo({
					url: '/package/setPassword/setPassword'
				})
			},
			handleTab(val) {
				console.log(val);
				this.defultCurrent = val.id
			},
			checkboxChange(val) {
				console.log(val);
			},
			// 验证码获取
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (!uni.$u.test.mobile(this.phone)) {
					uni.$u.toast('请输入正确的手机号！')
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					const data = {
						phone: this.phone
					}
					sendMsg({
						params: data
					}).then(res => {
						uni.hideLoading();
						if (res.code == 200) {
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
						}
					})
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			},
			rignUp(type) {
				console.log(this.policyValue);
				// if (this.policyValue.length == 0) {
				// 	uni.$u.toast('请先阅读并同意《用户协议》和《隐私政策》')
				// 	return
				// }
				uni.showLoading({
					title: '登陆中...',
					mask: true
				})
				if (type === 0) {
					// uni.$u.toast('账密登录')
					const data = {
						account: this.account,
						userPass: this.password
					}
					signIn({
						params: data
					}).then(res => {
						if (res.code == 200) {
							uni.setStorageSync('userInfo', res.data)
							uni.hideLoading()
							uni.switchTab({
								url: '/pages/index/index'
							})
						}
					})
				} else if (type === 1) {
					// uni.$u.toast('验证码登录')
					const data = {
						phone: this.phone,
						code: this.authcode,
						openId: this.openId
					}
					smsLogin(data).then(res => {
						if (res.code == 200) {
							uni.setStorageSync('userInfo', res.data)
							uni.hideLoading()
							if (res.data.password) {
								uni.reLaunch({
									url: '/pages/index/index'
								})
							} else {
								uni.navigateTo({
									url: ''
								})
							}
						}
					})
				}

			},
			weChatLogin() {
				var that = this
				if (!that.privacyState) {
					uni.$u.toast('请先阅读并同意《迪小星用户协议》和《隐私政策》')
					return
				}
				uni.showLoading({
					title: '登录中...',
					mask: true
				})
				uni.login({
					provider: 'weixin', // 指定使用微信登录
					success: function(loginRes) {
						if (loginRes.code) {
							// 登录成功后得到 code
							const code = loginRes.code;
							uni.hideLoading()
							// 将 code 发送到你的后端服务器，获取 openid
							const params = {
								code: code,
								dadId: uni.getStorageSync('merchantId')
							}
							console.log('params', params);
							getOpenId({
								params
							}).then(res => {
								if (res.code == 200) {
									uni.setStorageSync('token', res.data.token)
									uni.setStorageSync('userInfo', res.data)
									that.loginState = true;
									that.userInfo = res.data
									uni.reLaunch({
										url: '/pages/index/index'
									})
									if (that.userInfo.reviewStatus === 2) {
										uni.reLaunch({
											url: '/pages/index/index'
										})
									}
								}
							})
						} else {
							console.error('登录失败！' + loginRes.errMsg);
						}
					},
					fail: function(error) {
						console.error('微信登录失败', error);
						uni.hideLoading()
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.login {
		width: 100%;
		height: 100%;
		background: #FFFFFF;
		position: relative;

		.app-logo {
			width: 176rpx;
			height: 176rpx;
			position: absolute;
			top: 308rpx;
			left: 50%;
			transform: translateX(-50%);
		}

		.slogan {
			position: absolute;
			top: 520rpx;
			left: 50%;
			transform: translateX(-50%);
			font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
			font-weight: 600;
			font-size: 48rpx;
			color: #000000;
		}

		.loginLink {
			position: absolute;
			top: 570rpx;
			left: 41%;
			transform: translateX(-50%);
			width: 192rpx;
			height: 16rpx;
		}

		.loginBtn {
			width: 640rpx;
			height: 96rpx;
			line-height: 96rpx;
			background: linear-gradient(-49.9deg, #00bf39 0%, #34dfa3 100%);
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			position: absolute;
			top: 644rpx;
			left: 50%;
			transform: translateX(-50%);
			border: none;
			font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
		}

		.loginBtnBack {
			width: 640rpx;
			height: 96rpx;
			line-height: 96rpx;
			background: #000;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			position: absolute;
			top: 644rpx;
			left: 50%;
			transform: translateX(-50%);
			border: none;
			font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
		}

		.phoneBtn {
			width: 640rpx;
			height: 96rpx;
			line-height: 96rpx;
			background: #EFF7FF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
			font-weight: 400;
			font-size: 32rpx;
			color: #007FFF;
			position: absolute;
			top: 776rpx;
			left: 50%;
			transform: translateX(-50%);
			border: none;
		}

		.privacyBox {
			position: absolute;
			bottom: 68rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 80%;
			font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
			font-weight: 300;
			font-size: 24rpx;
			text-align: center;
		}
	}
</style>